{
 "cells": [
  {
   "metadata": {
    "ExecuteTime": {
     "end_time": "2025-09-02T07:09:42.360448Z",
     "start_time": "2025-09-02T07:09:42.353219Z"
    }
   },
   "cell_type": "code",
   "source": "from sklearn.preprocessing import MinMaxScaler",
   "id": "bf2b5d2aa0a8df9e",
   "outputs": [],
   "execution_count": 14
  },
  {
   "metadata": {
    "ExecuteTime": {
     "end_time": "2025-09-02T07:09:42.367858Z",
     "start_time": "2025-09-02T07:09:42.361669Z"
    }
   },
   "cell_type": "code",
   "source": "X = [[2,1],[3,1],[1,4],[2,6]]",
   "id": "e0cb224bec5b9a87",
   "outputs": [],
   "execution_count": 15
  },
  {
   "metadata": {
    "ExecuteTime": {
     "end_time": "2025-09-02T07:09:42.375467Z",
     "start_time": "2025-09-02T07:09:42.369387Z"
    }
   },
   "cell_type": "code",
   "source": [
    "#定义归一化类的对象\n",
    "scaler = MinMaxScaler(feature_range=(-1,1))\n",
    "#将缩放器应用到特征上\n",
    "X_scaled = scaler.fit_transform(X)\n",
    "print(X_scaled)"
   ],
   "id": "8b862ec5ad84c2e7",
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "[[ 0.  -1. ]\n",
      " [ 1.  -1. ]\n",
      " [-1.   0.2]\n",
      " [ 0.   1. ]]\n"
     ]
    }
   ],
   "execution_count": 16
  },
  {
   "metadata": {
    "ExecuteTime": {
     "end_time": "2025-09-02T07:09:42.382507Z",
     "start_time": "2025-09-02T07:09:42.375467Z"
    }
   },
   "cell_type": "code",
   "source": "from sklearn.preprocessing import StandardScaler",
   "id": "b203eea6ce1ba0bf",
   "outputs": [],
   "execution_count": 17
  },
  {
   "metadata": {
    "ExecuteTime": {
     "end_time": "2025-09-02T07:09:42.391680Z",
     "start_time": "2025-09-02T07:09:42.382507Z"
    }
   },
   "cell_type": "code",
   "source": [
    "#标准化\n",
    "scaler = StandardScaler()\n",
    "X_scaled = scaler.fit_transform(X)\n",
    "print(X_scaled)"
   ],
   "id": "23f4702da30c93f7",
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "[[ 0.         -0.94280904]\n",
      " [ 1.41421356 -0.94280904]\n",
      " [-1.41421356  0.47140452]\n",
      " [ 0.          1.41421356]]\n"
     ]
    }
   ],
   "execution_count": 18
  },
  {
   "metadata": {
    "ExecuteTime": {
     "end_time": "2025-09-02T07:09:42.399001Z",
     "start_time": "2025-09-02T07:09:42.391680Z"
    }
   },
   "cell_type": "code",
   "source": "import numpy as np",
   "id": "c297de228172238a",
   "outputs": [],
   "execution_count": 19
  },
  {
   "metadata": {
    "ExecuteTime": {
     "end_time": "2025-09-02T07:09:42.425198Z",
     "start_time": "2025-09-02T07:09:42.420202Z"
    }
   },
   "cell_type": "code",
   "source": [
    "X = np.array(X)\n",
    "print(X)"
   ],
   "id": "a3335060dd7c3d6a",
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "[[2 1]\n",
      " [3 1]\n",
      " [1 4]\n",
      " [2 6]]\n"
     ]
    }
   ],
   "execution_count": 20
  },
  {
   "metadata": {
    "ExecuteTime": {
     "end_time": "2025-09-02T07:09:42.448249Z",
     "start_time": "2025-09-02T07:09:42.435942Z"
    }
   },
   "cell_type": "code",
   "source": [
    "mean = np.mean(X,axis=0)\n",
    "print(mean)#按列求均值"
   ],
   "id": "40d2e6a6e7da007d",
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "[2. 3.]\n"
     ]
    }
   ],
   "execution_count": 21
  },
  {
   "metadata": {
    "ExecuteTime": {
     "end_time": "2025-09-02T07:09:42.456383Z",
     "start_time": "2025-09-02T07:09:42.450130Z"
    }
   },
   "cell_type": "code",
   "source": [
    "std = np.std(X,axis=0)\n",
    "print(std)"
   ],
   "id": "b99c3f57dfac9085",
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "[0.70710678 2.12132034]\n"
     ]
    }
   ],
   "execution_count": 22
  },
  {
   "metadata": {
    "ExecuteTime": {
     "end_time": "2025-09-02T07:09:42.462850Z",
     "start_time": "2025-09-02T07:09:42.456383Z"
    }
   },
   "cell_type": "code",
   "source": "print((X-mean)/std)",
   "id": "721f73e9348787c7",
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "[[ 0.         -0.94280904]\n",
      " [ 1.41421356 -0.94280904]\n",
      " [-1.41421356  0.47140452]\n",
      " [ 0.          1.41421356]]\n"
     ]
    }
   ],
   "execution_count": 23
  },
  {
   "metadata": {
    "ExecuteTime": {
     "end_time": "2025-09-02T07:09:42.468420Z",
     "start_time": "2025-09-02T07:09:42.462850Z"
    }
   },
   "cell_type": "code",
   "source": "",
   "id": "c026612f5cb16f1c",
   "outputs": [],
   "execution_count": 23
  }
 ],
 "metadata": {
  "kernelspec": {
   "display_name": "Python 3",
   "language": "python",
   "name": "python3"
  },
  "language_info": {
   "codemirror_mode": {
    "name": "ipython",
    "version": 2
   },
   "file_extension": ".py",
   "mimetype": "text/x-python",
   "name": "python",
   "nbconvert_exporter": "python",
   "pygments_lexer": "ipython2",
   "version": "2.7.6"
  }
 },
 "nbformat": 4,
 "nbformat_minor": 5
}
